package Stream;

import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Stream流对象的五种创建方式
 */
public class StreamGenerator {
    //基于数组创建
    @Test
    public void generator1() {
        String[] arr = {"Java", "C++", "Python", "WEB"};
        Stream<String> stream = Stream.of(arr);
        stream.forEach(s -> System.out.println(s));
    }

    //基于集合创建
    @Test
    public void generator2() {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("C++");
        list.add("Python");
        list.add("WEB");
        Stream<String> stream = list.stream();
        stream.forEach(s -> System.out.println(s));
    }

    //利用generator方法创建无线长度流
    @Test
    public void generator3() {
        Stream<Integer> stream = Stream.generate(() -> new Random().nextInt(100000));
        stream.limit(10).forEach(i -> System.out.println(i));
    }

    //4.基于送代器创建流
    @Test
    public void generator4() {
        Stream<Integer> stream = Stream.iterate(1, n -> n + 1);
        stream.limit(100).forEach(i -> System.out.println(i));

    }

    //基于字符序列创建流
    @Test
    public void generator5() {
        String str = "我爱你";
        IntStream stream = str.chars();
        stream.forEach(i -> System.out.println((char) i));

    }
}
